from UI.UI_new_launch_config            import Ui_dialog_new_config
from utils.utils                        import getUsefulPath
from utils.utils                        import getFileFromSystem
from utils.utils                        import setTheme
from PyQt4                              import QtGui
import json
import os

class NewConfigDial(QtGui.QDialog,Ui_dialog_new_config,QtGui.QWidget):
    def __init__(self, exeList=[], parent=None):
        super(NewConfigDial,self).__init__(parent=parent)
        self.setupUi(self)
        self.exeList = exeList
        if not self.exeList:
            usefulPath = getUsefulPath()
            files = os.listdir(usefulPath)
            names = []
            for fileName in files:
                if fileName.startswith('exe_list_'):
                    names.append(fileName)
            for name in names:
                self.exeList.extend(self.getExeFromFile(os.path.join(getUsefulPath(), name)))
        self.setEvents()
        self.initFields()
        setTheme(self)
    
    def getExeFromFile(self, filePath):
        if os.path.exists(filePath):
            target = open(filePath, 'r')
            jsonRes = target.read()
            target.close()
            return json.loads(jsonRes)
        return []
        
    def initFields(self):
        self.radioButton_from_list.setChecked(True)
        self.clicked_radio_from_list()
        self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        
    def setEvents(self):
        self.buttonBox.accepted.connect(self.acceptAskForm)
        self.buttonBox.rejected.connect(self.rejectAskForm)
        self.radioButton_from_list.clicked.connect(self.clicked_radio_from_list)
        self.radioButton_custom.clicked.connect(self.clicked_radio_custom)
        self.listWidget.itemClicked.connect(self.listItemClicked)
        self.lineEdit_search.textChanged.connect(self.editFilterRegExpChanged)
        self.horizontalSlider.sliderMoved.connect(self.changeTransparency)
        self.pushButton_browse.clicked.connect(self.browsePath)

    def editFilterRegExpChanged(self):
        stringToFilter = self.lineEdit_search.text()
        resList = filter(lambda k: str(stringToFilter).upper() in k.upper(), self.exeList)
        self.listWidget.clear()
        for element in resList:
            item = QtGui.QListWidgetItem(str(element))
            self.listWidget.addItem(item)
                
    def listItemClicked(self, item):
        self.lineEdit_out_path.setText(item.text())
    
    def clicked_radio_custom(self):
        self.listWidget.setHidden(True)
        self.label_search.setHidden(True)
        self.lineEdit_search.setHidden(True)
        self.pushButton_search.setHidden(True)
        self.pushButton_browse.setHidden(False)
        self.lineEdit_out_path.setHidden(False)
        self.label_out_path.setHidden(False)
    
    def clicked_radio_from_list(self):
        if self.listWidget.count() == 0:
            for element in self.exeList:
                item = QtGui.QListWidgetItem(str(element))
                self.listWidget.addItem(item)
        self.listWidget.setHidden(False)
        self.label_search.setHidden(False)
        self.lineEdit_search.setHidden(False)
        self.pushButton_search.setHidden(False)
        self.pushButton_browse.setHidden(True)
        self.lineEdit_out_path.setHidden(True)
        self.label_out_path.setHidden(True)

    def changeTransparency(self, value=0):
        outVal = 100 - value
        self.setWindowOpacity(outVal/100.)
        
    def acceptAskForm(self):
        self.accept()
        
    def rejectAskForm(self):
        self.reject()

    def browsePath(self):
        newPath = getFileFromSystem()
        self.lineEdit_out_path.setText(newPath)
        